// #include "stdafx.h"

// #include <stdio.h>
// #include <tchar.h>
// #include <iostream>
// #include <string.h>
// #include <windows.h>
#include "saleTickets.h"

extern HANDLE g_hMutex;
extern bool is_run;

DWORD WINAPI saleStickets(LPVOID lpParameter)
{
  THREAD_DATA *data = (THREAD_DATA *)lpParameter;
  Ticket *p_tickets = data->p_tickets;

  while (p_tickets->num > 0)
  {
    WaitForSingleObject(g_hMutex, INFINITE); // 请求获得一个互斥量锁
    if (p_tickets->num > 0)
    {
      std::cout << data->name << "出售第" << p_tickets->num-- << "张票，";
      if (p_tickets->num >= 0)
      {
        std::cout << "出票成功！剩余" << p_tickets->num << "张票！" << std::endl;
      }
      else
      {
        std::cout << "出票失败！该票已售完！" << std::endl;
      }
    }
    Sleep(10);
    ReleaseMutex(g_hMutex); // 释放互斥锁
  }
  // std::cout << data->name << "关闭！！！" << std::endl;
  is_run = false;
  return 0L;
}